import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)


// 确保路由传参时，重复传参不会报错
// 例如：this.$router.push({name:"search",params:{keyword:this.keyword},query:{k:this.keyword.toUpperCase()}},()=>{},()=>{}) 
// 可以不用加后面的两个()=>{}来控制resolve和reject
let originPush = VueRouter.prototype.push;
VueRouter.prototype.push = function(location,resolve,reject){
    if(resolve && reject){
        //call || apply区别
        //相同点：都可以调用函数一次，可以篡改函数的上下文一次
        //不同点：call与apply传递参数，call传递参数用逗号隔开，apply用数组传递参数
        originPush.call(this,location,resolve,reject)
    }
    else{
        originPush.call(this,location,()=>{},()=>{})
    }
}

const routes = [
    {
        path: '/',
        name: 'Main',
        component: () => import('@/views/Main.vue'),
        children: [
            {
                path:'/creditRating',
                name:'creditRating',
                component: () => import('@/views/creditRating'),
                meta:{
                    show:true
                }
            },
            {
                path:'/calculator',
                name:'calculator',
                component: () => import('@/views/calculator'),
                meta:{
                    show:true
                }
            },
            {
                path:'/recommend',
                name:'recommend',
                component: () => import('@/views/recommend'),
                meta:{
                    show:true
                }
            },
            {
                path:'/partner',
                name:'partner',
                component: () => import('@/views/partner'),
                meta:{
                    show:true
                }
            }
        ]
    },
    {
        path:'/login',
        name:'login',
        component: () => import('@/views/login'),
        meta:{
            show:false
        }
    },
    {
        path:'/register',
        name:'register',
        component: () => import('@/views/register'),
        meta:{
            show:false
        }
    }
]

const router = new VueRouter({
    mode: 'history',
    routes
})

export default router